﻿using DBUtil.Provider.MySql;
using log4net;
using Sunny.UI;
using System.Configuration;

namespace FZRD
{
    //[assembly: log4net.Config.XmlConfigurator(Watch = true)]
    public partial class MainForm : UIHeaderMainFrame
    {
        private static readonly ILog log = LogManager.GetLogger(typeof(MainForm));

        public static string mySqlDBUrl;


        public static MySqlAccess db;

        public bool isAdmin;


        public MainForm(bool isAdmin)
        {
           log4net.Config.XmlConfigurator.Configure(new FileInfo("log4net.config"));
            this.isAdmin = isAdmin;

            InitializeComponent();

            mySqlDBUrl = ConfigurationManager.AppSettings["MySqlDBUrl"];

            DBUtil.DBFactory.AddDBSupport<DBUtil.Provider.MySql.MySqlDBFactory>();
            db = MySqlDBFactory.CreateDB(mySqlDBUrl,
                DBUtil.DBSetting.NewInstance().SetSqlMonitor(arg =>
                {
                    log.Info(arg);
                    return Task.CompletedTask;
                })
            );

            //设置关联
            Header.TabControl = MainTabControl;

            //增加页面到Main
            AddPage(new Page1(), 1001);
            AddPage(new Page2(), 1002);
            AddPage(new Page3(), 1003);
            AddPage(new Page4(), 1004);
            if (isAdmin)
            {
                AddPage(new Page5(), 1005);
                AddPage(new Page6(), 1006);
                AddPage(new Page7(), 1007);
            }

            TreeNode treeNode1 = new TreeNode("波形显示");
            TreeNode treeNode2 = new TreeNode("历史查询");
            TreeNode treeNode3 = new TreeNode("告警通知");
            TreeNode treeNode4 = new TreeNode("操作日志");
            TreeNode treeNode5 = new TreeNode("设备管理");
            TreeNode treeNode6 = new TreeNode("用户管理");
            TreeNode treeNode7 = new TreeNode("参数设置");
            treeNode1.Name = "波形显示";
            treeNode1.Text = "波形显示";
            treeNode2.Name = "历史查询";
            treeNode2.Text = "历史查询";
            treeNode3.Name = "告警通知";
            treeNode3.Text = "告警通知";
            treeNode4.Name = "操作日志";
            treeNode4.Text = "操作日志";
            treeNode5.Name = "设备管理";
            treeNode5.Text = "设备管理";
            treeNode6.Name = "用户管理";
            treeNode6.Text = "用户管理";
            treeNode7.Name = "参数设置";
            treeNode7.Text = "参数设置";
            Header.Nodes.AddRange(new TreeNode[] { treeNode1, treeNode2, treeNode3, treeNode4 });
            //设置Header节点索引
            Header.SetNodePageIndex(Header.Nodes[0], 1001);
            Header.SetNodePageIndex(Header.Nodes[1], 1002);
            Header.SetNodePageIndex(Header.Nodes[2], 1003);
            Header.SetNodePageIndex(Header.Nodes[3], 1004);
            if (isAdmin)
            {
                Header.Nodes.AddRange(new TreeNode[] { treeNode5 });
                Header.SetNodePageIndex(Header.Nodes[4], 1005);
                Header.Nodes.AddRange(new TreeNode[] { treeNode6 });
                Header.SetNodePageIndex(Header.Nodes[5], 1006);
                Header.Nodes.AddRange(new TreeNode[] { treeNode7 });
                Header.SetNodePageIndex(Header.Nodes[6], 1007);
            }

            //显示默认界面
            Header.SelectedIndex = 0;

        }

        public bool IsAdmin()
        {
            return isAdmin;
        }

        private void MainForm_Load(object sender, EventArgs e)
        {

        }

        //窗口关闭事件委托，在窗口关闭事件前处理
        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            string userIp = "127.0.0.1";
            long recordTime = ((DateTimeOffset)DateTime.UtcNow).ToUnixTimeMilliseconds();
            try
            {
                MainForm.db.ExecuteSql(
                MainForm.db.Insert("system_operation_content")
                    .SetColumn("UserName", LoginForm.username)
                    .SetColumn("UserIP", userIp)
                    .SetColumn("RecordTime", recordTime)
                    .SetColumn("Operation", "登出")
                    .ToSql());
                MainForm.db.ExecuteSql(
                    MainForm.db.Update("system_user")
                    .SetColumn("LogoutTime", recordTime)
                    .Where("UserName='" + LoginForm.username + "'")
                    .ToSql());

                //if (serverSocket != null)
                //{
                //    if (serverSocket.Connected)
                //    {
                //        serverSocket.Disconnect(false);
                //    }
                //    serverSocket.Close();
                //    serverSocket.Dispose();
                //}
                //if (receiveThread != null)
                //{
                //    receiveThread.Interrupt();
                //}
            }
            catch (Exception ex)
            {
                log.Error(ex.Message);
            }
        }

        public static long ToUnixTimestamp(DateTime dateTime)
        {
            return (long)Math.Floor((dateTime.ToUniversalTime() - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalSeconds);
        }

        public static long ToUnixTimestampMilliseconds(DateTime dateTime)
        {
            return (long)(dateTime.ToUniversalTime() - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalMilliseconds;
        }

    }
}